package com.gxa.weixing.pc.controller;

import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.util.OssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 图片上传的接口
 *
 * @author YXQ
 * @date 2021/10/21 20:55
 */
@CrossOrigin
@RestController
@Api(value = "上传文件", tags = "上传文件的接口")
public class UploadController {

    @PostMapping("/upload")
    @ApiOperation("上传单个文件到本地服务器")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        System.out.println("0000000000000");
        //QQ截图20200724153059.png
        String originalFilename = multipartFile.getOriginalFilename();
        // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
        String suffix = originalFilename.substring(originalFilename.indexOf(".") + 1);
        //使用UUID作为文件名,保证服务器中存放的文件名唯一
        String targetFile = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
        // 通过request获取当前程序的根路径"/"
        String path = request.getServletContext().getRealPath("/");
        String fileName = path + "/temp/" + targetFile;
        String urlFile = "/temp/" + targetFile;
        // 构建文件对象
        File uploadFile = new File(fileName);
        //核心操作(含有异常操作, 利用异常设定状态码(和前端约定)
        // 此处应当和ExceptionController结合, 当try执行成功则设定状态码0
        // 出现异常则设定其他
        // 再将异常信息结合BusinessException抛出, 交给ExceptionController处理
        try {
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            //e.printStackTrace();
            throw e;
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("上传成功");
        jsonResult.put("fileName", urlFile);
        return jsonResult.getDataMap();
    }

    @PostMapping("/uploads")
    @ApiOperation("多文件上传到本地服务器")
    public Map<String, Object> uploads(@RequestParam("file") MultipartFile[] multipartFile, HttpServletRequest request) throws IOException {
        List<String> lists = new ArrayList<>();
        for (MultipartFile file : multipartFile) {
            //QQ截图20200724153059.png
            String originalFilename = file.getOriginalFilename();
            // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
            String suffix = originalFilename.substring(originalFilename.indexOf(".") + 1);
            //使用UUID作为文件名,保证服务器中存放的文件名唯一
            String targetFile = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
            // 通过request获取当前程序的根路径"/"
            String path = request.getServletContext().getRealPath("/");
            String fileName = path + "/temp/" + targetFile;
            // 给前端返回的路径名
            String urlPath = "/temp/" + targetFile;
            // 构建文件对象
            File uploadFile = new File(fileName);
            //核心操作(含有异常操作, 利用异常设定状态码(和前端约定)
            // 此处应当和ExceptionController结合, 当try执行成功则设定状态码0
            // 出现异常则设定其他
            // 再将异常信息结合BusinessException抛出, 交给ExceptionController处理
            try {
                file.transferTo(uploadFile);
                lists.add(urlPath);
            } catch (IOException e) {
                //e.printStackTrace();
                throw e;
            }
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("上传成功");
        jsonResult.put("fileNames", lists);
        return jsonResult.getDataMap();
    }

    @PostMapping("/uploadToOss")
    @ApiOperation("上传单个文件到云服务器")
    public Map<String, Object> uploadToOss(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        //QQ截图20200724153059.png
        String originalFilename = multipartFile.getOriginalFilename();
        // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        //使用UUID作为文件名,保证服务器中存放的文件名唯一
        String targetFile = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
        // 通过request获取当前程序的根路径"/"
        // 把文件直接上传到服务器的指定位置
        String fileName = "wisdomShopping/uploadImg/" + targetFile;
        // 构建文件对象
        OssUtil.upload(fileName, multipartFile.getInputStream());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("上传成功");
        jsonResult.put("fileName",  targetFile);
        // jsonResult.put("fileName", "/ossLearning/" + targetFile);
        jsonResult.put("url", "http://bucket-lijianmin.oss-cn-chengdu.aliyuncs.com/" + fileName);
        System.out.println(jsonResult.getDataMap());
        return jsonResult.getDataMap();
    }

    @PostMapping("/uploadToOsss")
    @ApiOperation("上传单个文件到云服务器")
    public Map<String, Object> uploadToOss(@RequestParam("file") MultipartFile multipartFile[], HttpServletRequest request) throws IOException {
        JsonResult jsonResult = new JsonResult();
        for (int i = 0; i < multipartFile.length; i++) {
            //QQ截图20200724153059.png
            String originalFilename = multipartFile[i].getOriginalFilename();
            // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
            String suffix = originalFilename.substring(originalFilename.indexOf(".") + 1);
            //使用UUID作为文件名,保证服务器中存放的文件名唯一
            String targetFile = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
            // 通过request获取当前程序的根路径"/"
            // 把文件直接上传到服务器的指定位置
            String fileName = "wisdomShopping/uploadImg/" + targetFile;
            // 构建文件对象
            OssUtil.upload(fileName, multipartFile[i].getInputStream());
            jsonResult.put("fileName"+i,  targetFile);
            jsonResult.put("url"+ i, "http://bucket-lijianmin.oss-cn-chengdu.aliyuncs.com/" + fileName);
        }

        jsonResult.setCode("1");
        jsonResult.setMsg("上传成功");

        // jsonResult.put("fileName", "/ossLearning/" + targetFile);
        return jsonResult.getDataMap();
    }

    @PostMapping("/uploadTrainPlanDocumentToOss")
    @ApiOperation("上传培训计划的单独的接口,之前为了测试redis单独写的方法")
    public Map<String, Object> uploadDocumentToOss(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws IOException {
        //QQ截图20200724153059.png
        String originalFilename = multipartFile.getOriginalFilename();
        // 从原始文件名中获取后缀,作为新添加的文件的文件名后缀(匹配)
        String suffix = originalFilename.substring(originalFilename.indexOf(".") + 1);
        //使用UUID作为文件名,保证服务器中存放的文件名唯一
        String targetFile = UUID.randomUUID().toString().replace("-", "") + "." + suffix;
        // 通过request获取当前程序的根路径"/"
        // 把文件直接上传到服务器的指定位置
        String fileName = "learning/trainPlanDocument/temp/" + targetFile;
        // 构建文件对象
        OssUtil.upload(fileName, multipartFile.getInputStream());
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("上传成功");
        jsonResult.put("fileName", "/ossTrainPlanDocument/" + targetFile);
        jsonResult.put("url", "http://bucket-yanxiaoqi.oss-cn-chengdu.aliyuncs.com/" + fileName);
        return jsonResult.getDataMap();
    }

}
